<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Zhilvyun_Api_Ticket
{
    private static $_typeid = 5;

    /**
     * 获取门票列表
     * @return mixed
     */
    public static function get_ticket_list()
    {
        $list = DB::select()->from('distribute_zhilvyun_product')->where('typeid','=',5)->execute()->as_array();
        $out = array();
        foreach($list as $row)
        {
            $ticket = array(
                'title' => $row['suit_title'],
                'ticket_id' => $row['suit_id'],
                'product_id' => $row['product_id'],
            );
            $out[] = $ticket;
        }
        return $out;
    }
    /**
     * 获取门票详情
     * @param $params
     * @return mixed
     */
    public static function get_ticket_detail($params)
    {
        //套餐信息
        $suit_info = DB::select()->from('spot_ticket')
            ->where('id','=',$params['ticket_id'])
            ->and_where('spotid','=',$params['product_id'])
            ->execute()->current();

        $suit = array(
            'title' => $suit_info['title'],
            'ticket_id' => $suit_info['id'],
            'product_id' => $suit_info['spotid'],
            'description' => $suit_info['description'],
            'day_before' => $suit_info['day_before'],
            'hour_before' => $suit_info['hour_before'],
            'refund_restriction' => $suit_info['refund_restriction'],
            'fill_tourer_type' => $suit_info['fill_tourer_type'],
            'fill_tourer_items' => $suit_info['fill_tourer_items'],
            'effective_days' => $suit_info['effective_days']
        );
        return $suit;

    }


    /**
     * 获取报价日历
     * @param $params
     * @return array
     */
    public static function get_calendar($params,$zhilvyun='zhilvyun')
    {
        $start_date = strtotime($params['startTime']);
        $end_date = strtotime($params['endTime']);
        $ticket_id = abs($params['partnerDealId']);
        $data = array();
        $row = DB::select()->from('distribute_'.$zhilvyun.'_product')
            ->where('suit_id','=',$ticket_id)
            ->and_where('typeid','=',self::$_typeid)
            ->execute()->current();
        for ($current_date = $start_date; $current_date <= $end_date; $current_date += 86400)
        {
            $ticket_storage =DB::select('number')->from('spot_ticket_price')
                ->where('ticketid','=',$ticket_id)
                ->and_where('day','=',$current_date)
                ->execute()
                ->current();

            if(isset($ticket_storage['number']) && ($ticket_storage['number']== -1 || $ticket_storage['number']>0))
            {
                $day = date('Y-m-d',$current_date);
                $data[] = array(
                    'partnerDealId' => $ticket_id,
                    'date' => $day,
                    'price' => $row['list_price'],
                    'marketPrice' => $row['sell_price'],
                    'settlementPrice' => $row['settle_price'],
                    'stock' => $ticket_storage['number'] == -1 ? 99999 : $ticket_storage['number']
                );
            }
        }
        return $data;
    }
    /**
     * 减库存
     * @param $params
     */
    public static function storage($params)
    {
       return  Model_Spot::storage($params['ticket_id'],$params['number'],array('usedate'=>$params['use_date']));

    }



    /**
     * 获取消费码状态
     * @param $status 0,未使用 1,已使用 6,已退款
     */
    public static function get_eticket_num($order_id,$status)
    {
        $eticket = DB::select('eticket','status')
            ->from('member_order_eticket')
            ->where('order_id','=',$order_id)
            ->and_where('status','=',$status)
            ->execute()->as_array();
        return count($eticket);
    }




}